package com.cssw.fyzb.busi.api.acct;

import com.cssw.fyzb.busi.service.acct.OutAcctService;
import com.cssw.fyzb.core.exception.BizException;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: yangyiqun
 * @Date: 2020/7/11 16:25
 */
@Function(description = "保存银行卡信息", parameters = {
        @Parameter(name = "bankId", type = Type.String, description = "银行信息标识", required = false),
        @Parameter(name = "userId", type = Type.String, description = "用户标识", required = true),
        @Parameter(name = "realName", type = Type.String, description = "姓名", required = true),
        @Parameter(name = "cardNo", type = Type.String, description = "卡号", required = true),
        @Parameter(name = "bankName", type = Type.String, description = "银行名称", required = true),
        @Parameter(name = "openBank", type = Type.String, description = "开户行", required = true),
        @Parameter(name = "defaultFlag", type = Type.Integer, description = "是否默认（1-是 0-否）", required = true),
        @Parameter(name = "city", type = Type.String, description = "市", required = true),
        @Parameter(name = "province", type = Type.String, description = "省", required = true),
})
@Component
public class SaveBankPay implements Action {
    private static final Logger logger = LoggerFactory.getLogger(SaveBankPay.class);

    @Autowired
    private OutAcctService outAcctService;

    @Override
    public Response execute(Request request) throws Exception {
        Long bankId = request.getLong("bankId");
        Long userId = request.getLong("userId");
        String cardNo = request.getString("cardNo");
        String name = request.getString("realName");
        String bankName = request.getString("bankName");
        String openBank = request.getString("openBank");
        int defaultFlag = request.getInteger("defaultFlag");
        String province = request.getString("province");
        String city = request.getString("city");
        String bankCode = request.getString("bankCode");
        try {
            outAcctService.saveBank(bankId, userId, bankName, cardNo, openBank, name, defaultFlag,province,city,bankCode);
        } catch (BizException be) {
            logger.error(be.getErrMsg(), be.getErrCode());
            return Response.fail(be.getErrMsg(), be.getErrCode());
        } catch (Exception e) {
            logger.error("", e);
            return Response.fail("保存银行卡信息失败");
        }
        return Response.success();
    }
}
